package cn.wolfcode.luowowo.mgrsite.Listener;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IScenicCommentCacheService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.ScenicCommentStats;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.cache.vo.TravelStats;
import cn.wolfcode.luowowo.scenic.domain.ScenicComment;
import cn.wolfcode.luowowo.scenic.service.IScenicCommentService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import javax.naming.Context;
import java.util.List;

/**
 * 统计数据初始化监听器
 * 接口中的泛型ContextRefreshedEvent:表示Spring创建bean结束时的事件
 */
@Component
public class StatsDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelStatsCacheService travelStatsCacheService;
    @Reference
    private IScenicCommentCacheService scenicCommentCacheService;
    @Reference
    private IScenicCommentService scenicCommentService;

    @Value("${data.init}")
    private boolean init;

    //Spring容器创建bean完成后马上调用
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (init) {
            //完成统计数据的初始化,就是把mysql中的热数据搬到redis
            //1.查询所有文章对象
            List<StrategyDetail> list = strategyDetailService.list();
            //2.迭代每个对象取出热数据
            for (StrategyDetail detail : list) {
                //redis中没有该对象的key,才进行存储
                if (!strategyStatsCacheService.hasStateKey(detail.getId())) {
                    Long detailId = detail.getId();
                    //封装成统计VO对象
                    StrategyStats vo = new StrategyStats();
                    vo.setStrategyId(detailId);
                    vo.setViewnum(detail.getViewnum());
                    vo.setReplynum(detail.getReplynum());
                    vo.setFavornum(detail.getFavornum());
                    vo.setSharenum(detail.getSharenum());
                    vo.setThumbsupnum(detail.getThumbsupnum());
                    vo.setSharenum(detail.getSharenum());

                    Destination dest = detail.getDest();
                    vo.setDestId(dest.getId());
                    vo.setDestName(dest.getName());
                    vo.setTitle(detail.getTitle());
                    //保存到redis
                    strategyStatsCacheService.setStrategyStats(vo);
                }

                //飙升排行榜的数据初始化
                if (!strategyStatsCacheService.exist(RedisKey.STRATEGY_STATS_UP_SORT, detail.getId())) {
                    int num = detail.getViewnum() + detail.getReplynum();
                    strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_UP_SORT, num, detail.getId());
                }

                //热门排行榜的数据初始化
                if (!strategyStatsCacheService.exist(RedisKey.STRATEGY_STATS_HOT_SORT, detail.getId())) {
                    int num = detail.getFavornum() + detail.getThumbsupnum();
                    strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_HOT_SORT, num, detail.getId());
                }
            }
            //完成统计数据的初始化,就是把mysql中的热数据搬到redis
            //1.查询所有游记对象
            List<Travel> list2 = travelService.list();
            //2.迭代每个对象取出热数据
            for (Travel travel : list2) {
                //redis中没有该对象的key,才进行存储
                if (!travelStatsCacheService.hasStateKey(travel.getId())) {
                    Long travelId = travel.getId();
                    //封装成统计VO对象
                    TravelStats vo = new TravelStats();
                    vo.setTravelId(travelId);
                    vo.setViewnum(travel.getViewnum());
                    vo.setReplynum(travel.getReplynum());
                    vo.setFavornum(travel.getFavornum());
                    vo.setSharenum(travel.getSharenum());
                    vo.setThumbsupnum(travel.getThumbsupnum());
                    vo.setSharenum(travel.getSharenum());

                    vo.setTodayviewnum(0);

                    Destination dest = travel.getDest();
                    vo.setDestId(dest.getId());
                    vo.setDestName(dest.getName());
                    vo.setTitle(travel.getTitle());

                    //保存到redis
                    travelStatsCacheService.setTravelStats(vo);
                }
              /*  //飙升排行榜的数据初始化
                if (!strategyStatsCacheService.exist(RedisKey.STRATEGY_STATS_UP_SORT,travel.getId())) {
                    int num = travel.getViewnum() + travel.getReplynum();
                    strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_UP_SORT, num, travel.getId());
                }

                //热门排行榜的数据初始化
                if (!strategyStatsCacheService.exist(RedisKey.STRATEGY_STATS_HOT_SORT,travel.getId())) {
                    int num = travel.getFavornum() + travel.getThumbsupnum();
                    strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_HOT_SORT, num, travel.getId());
                }*/
            }
            //点评数据初始化
            List<ScenicComment> ScenicComments = scenicCommentService.list();
            for (ScenicComment scenicComment : ScenicComments) {
                if (!scenicCommentCacheService.hasStatsKey(scenicComment.getId())) {
                    ScenicCommentStats scenicCommentStats = new ScenicCommentStats();

                    Integer thumbupnum = scenicComment.getThumbupnum();

                    scenicCommentStats.setCommentId(scenicComment.getId());
                    scenicCommentStats.setThumbsupnum(thumbupnum);
                    scenicCommentCacheService.setCommentStats(scenicCommentStats);
                }

            }
        }
    }
}
